package com.dq.Week2.day09.pritice;

import java.util.ArrayList;
import java.util.List;

public class Duel {
    public static void main(String[] args) {
        // Create three duelists for each team
        List<Person> team1 = new ArrayList<>();
        team1.add(new Person("Alice", 100, 20));
        team1.add(new Person("Bob", 100, 25));
        team1.add(new Person("Charlie", 100, 30));

        List<Person> team2 = new ArrayList<>();
        team2.add(new Person("David", 100, 20));
        team2.add(new Person("Eve", 100, 25));
        team2.add(new Person("Frank", 100, 30));

        // Dueling loop
        boolean isGameOver = false;
        while (!isGameOver) {
            // Each team member attacks a random member of the opposite team
            for (Person person : team1) {
                int targetIndex = (int) (Math.random() * team2.size());
                person.attack(team2.get(targetIndex));
                if (team2.get(targetIndex).getHealth() <= 0) {
                    team2.remove(targetIndex);
                }
            }

            // If one team has no members left, the game is over
            if (team2.isEmpty()) {
                isGameOver = true;
                System.out.println("Team 1 wins!");
            } else {
                for (Person person : team2) {
                    int targetIndex = (int) (Math.random() * team1.size());
                    person.attack(team1.get(targetIndex));
                    if (team1.get(targetIndex).getHealth() <= 0) {
                        team1.remove(targetIndex);
                    }
                }

                // Check if Team 2 has no members left
                if (team1.isEmpty()) {
                    isGameOver = true;
                    System.out.println("Team 2 wins!");
                }
            }
        }
    }
}